package cn.codergege.demo.controller;


import cn.codergege.demo.dao.DepartmentDao;
import cn.codergege.demo.dao.EmployeeDao;
import cn.codergege.demo.domain.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    private Logger log = LoggerFactory.getLogger(EmployeeController.class);

    @ModelAttribute
    public void queryEmployee(@RequestParam(value = "id", required = false) Integer id,
                              Map<String, Object> map) {
        // 如果 @RequestParam 的 id 不为空, 说明是 PUT 请求
        if (null != id) {
            Employee employee = employeeDao.getById(id);
            map.put("employee", employee);
        }
    }

    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private EmployeeDao employeeDao;

    // forward to /WEB-INF/templates/employee/list.html
    @RequestMapping("/emps")
    public String emps(Map<String, Object> map) {
        map.put("employees", employeeDao.getAll());
        return "employee/list";
    }

    // forward to /WEB-INF/templates/employee/add-page.html
    @RequestMapping("/emp")
    public String addPage(Map<String, Object> map) {
        fillMap(map);
        // 需要一个 model object
        map.put("employee", new Employee());

        return "employee/add-page";
    }

    // Add an employee
    @RequestMapping(value = "/emp", method = RequestMethod.POST)
    public String add(@Valid Employee employee, BindingResult result,
                      Map<String, Object> map) {
        if (result.hasErrors()) {
            log.info(result.getAllErrors().toString());
            fillMap(map);
            return "employee/add-page";
        }
        try {
            employeeDao.add(employee);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/employee/emps";
    }

    // Forward to /WEB-INF/templates/employee/update-page.html
    @RequestMapping("/emp/{id}")
    public String updatePage(@PathVariable("id") Integer id, Map<String, Object> map) {
        Employee employee = employeeDao.getById(id);
        map.put("employee", employee);
        fillMap(map);
        return "employee/update-page";
    }

    // Update employoee
    @RequestMapping(value = "emp", method = RequestMethod.PUT)
    public String update(@Valid Employee employee,
                         BindingResult result,
                         Map<String, Object> map) {
        if (result.hasErrors()) {
            log.info(result.getAllErrors().toString());
            fillMap(map);
            return "employee/update-page";
        }
        try {
            employeeDao.update(employee);
        } catch (Exception e) {
            e.printStackTrace();
            // todo: to error page
        }
        return "redirect:/employee/emps";
    }

    // Delete an Employee
    @RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integer id) {
        try {
            employeeDao.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            // todo: to error page
        }
        return "redirect:/employee/emps";
    }

    private void fillMap(Map<String, Object> map) {
        map.put("departments", departmentDao.getAll());
        Map<String, Integer> genderMap = new HashMap<>();
        genderMap.put("男", 1);
        genderMap.put("女", 0);
        map.put("genders", genderMap);

    }

}
